﻿Imports System.Data.OleDb
Imports System.IO
Imports unpassword2

Public Class ResetPassword_FORM
    Public Oracle_Uid_reset As String = Nothing
    Public site_schema_name As String = Nothing
    Public sitedd_schema_name As String = Nothing
    Public SqlplusScript As String = Nothing

    Dim Reset_User As String
    Dim Reset_Password As String

    Public conn As OleDbConnection
    Public comm As OleDbCommand
    Public Adp As OleDbDataAdapter
    Private Sub Connect_Click(sender As Object, e As EventArgs) Handles Connect.Click

        Reset_User = User.Text

        Reset_Password = Password.Text

        '查询所有的oracle db username
        Me.ListBox1.Items.Clear()
        Call OraConn_Reset(Oracle_Uid_reset, Me.PlantNeedReset.Text)

        For i = 0 To dtCreate.Rows.Count - 1
            Me.ListBox1.Items.Add(i)
            Me.ListBox1.Items(i) = dtCreate.Rows(i)(0) + vbTab + Encryptor.Decrypt(dtCreate.Rows(i)(3).ToString)
        Next
        Me.ListBox1.Items.Add(dtCreate.Rows.Count - 1 + 1)
        Me.ListBox1.Items(dtCreate.Rows.Count - 1 + 1) = site_schema_name
        Me.ListBox1.Items.Add(dtCreate.Rows.Count - 1 + 2)
        Me.ListBox1.Items(dtCreate.Rows.Count - 1 + 2) = sitedd_schema_name

    End Sub

    '重置密码时 的数据库连接，用的系统管理员,仅做了Oracle的，系统管理员的schema
    Public Sub OraConn_Reset(service_name As String, PlantNeedReset As String)
        Dim strConnection As String
        'data source是ORACLE CLIENT网络服务名
        strConnection = "Data source=" + service_name + ";uid=" + Reset_User + ";pwd=" + Reset_Password

        Try
            conn = New OleDbConnection(strConnection)

            '查询语句，SPIDM为目标数据库的domainschema的名字
            SelectStr = <string>select A.NAME,A.PASSWORD,B.SP_SCHEMA_TYPE,B.PASSWORD from sys.user$ A 
JOIN INGR.T_DB_DATA B ON A.NAME =UPPER(B.USERNAME)
order by b.sp_schema_type
            </string>.Value
            '  这里要改，因为INGR.是不确定的
            SelectStr = SelectStr.Replace("INGR", PlantNeedReset)
            'SelectStr = <string>select * from xxxSiteName.t_db_data
            '</string>.Value
            'SelectStr = SelectStr.Replace("xxxSiteName", Site_Schema)

            Adp = New OleDbDataAdapter(SelectStr, conn)

            dtCreate.Reset()
            ' On Error GoTo TestFailure2                                                                  '数据库连接失败的话，下一句fill会失败
            Adp.Fill(dtCreate)
            MessageBox.Show("连接成功")
            conn = Nothing

            Adp = Nothing
            Exit Sub
            'TestFailure2:
        Catch ex As Exception
            MessageBox.Show("数据库连接失败,请重试")
            MsgBox("有错  " + ex.Message)
        End Try
    End Sub
    '重置密码，调用cmd，读取sql文件
    Public Sub ResetPS(service_name As String, FilePath As String)
        Try
            Dim intResult As Integer
            intResult = MessageBox.Show("是否确认要重置密码", "Warning！", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

            If intResult = DialogResult.OK Then
                Shell("cmd /c sqlplus " & Reset_User + "/" & Reset_Password & "@" & service_name & " @" & FilePath)   '因为是异步执行，所以会直接同时去运行下一行语句
                MsgBox("修改成功")

            End If

            If intResult = DialogResult.Cancel Then

            End If

        Catch ex As Exception
            MessageBox.Show("数据库连接失败,请重试")
            MsgBox("有错  " + ex.Message)
        End Try
    End Sub
    '打开原始的ini，获取系统管理员用户和service name
    Private Sub Origin_ini_Click(sender As Object, e As EventArgs) Handles Origin_ini.Click
        '打开要重置密码的那个ini文件
        Dim ReadINI As FileDialog = New OpenFileDialog

        With ReadINI
            .AddExtension = True
            .Filter = "SmartPlant Manager V4 INI File|smartplantv4.ini"
            .FilterIndex = 1
            '换了一个ini
            If .ShowDialog = DialogResult.OK Then

                User.Text = MyPIDConn.getStrFromINI("SITESCHEMA", "DBAUSER", "Error", .FileName)
                site_schema_name = MyPIDConn.getStrFromINI("SITESCHEMA", "UID", "Error", .FileName) + vbTab + Encryptor.Decrypt(MyPIDConn.getStrFromINI("SITESCHEMA", "PWD", "Error", .FileName)) 'SITE Schema的帐号
                sitedd_schema_name = MyPIDConn.getStrFromINI("DICTIONARY", "UID", "Error", .FileName) + vbTab + Encryptor.Decrypt(MyPIDConn.getStrFromINI("DICTIONARY", "PWD", "Error", .FileName)) 'SITE Schema的帐号
                Oracle_Uid_reset = MyPIDConn.getStrFromINI("SITESCHEMA", "ORACLE_SERVICE", "Error", .FileName) 'ini中的网络服务
                Connect.Enabled = True


            End If
        End With

    End Sub

    '创建一个sql，用于后面cmd执行sqlplus
    Private Sub Reset_Click(sender As Object, e As EventArgs) Handles Reset.Click
        Dim sw As StreamWriter = New StreamWriter("c:\PPM_PID_ResetPS.sql", False)
        sw.WriteLine(SqlplusScript)
        sw.Flush()
        sw.Close()
        Call ResetPS(Oracle_Uid_reset, "c:\PPM_PID_ResetPS.sql")
    End Sub
    '获得sql文件中的alter指令
    Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedItem >= 0 Then


            SqlplusScript = ListBox1.SelectedItem
            SqlplusScript = "alter user " + Trim(Mid(SqlplusScript, 1, InStr(SqlplusScript, vbTab))) + " identified by " + Trim(Mid(SqlplusScript, InStr(SqlplusScript, vbTab))) + ";"
            Reset.Enabled = True
        Else

            Reset.Enabled = False
        End If
    End Sub


End Class